<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #app{
            width: 400px;
            padding: 10px;
            background-color: rgb(248, 242, 237);
        }
        h3{
            text-align: center;
        }
        table{
            border-collapse: collapse;
            width: 400px;
        }
        table tr td{
            border:1px solid gray;  
        }
        button{
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <div id="app">
        <!-- 2.dom渲染出来 -->
        <!-- 3.双向绑定 -->
        <!-- 4.绑定事件 -->
        <h3>日常工作列表：</h3>
        <input type="text" v-model="uu" placeholder="请输入日常" />
        <button @click="adduser">添加日常</button>
        <table>
            <tr>
                <td>序号</td>
                <td>日常</td>
                <td>删除日常</td>
            </tr>
            <tr v-for="item in stu" :key="item.id">
                <!-- 6.绑定删除的函数 -->
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>
                    <button @click="delFunc(index)">×</button>
                </td>   
            </tr>
        </table>

    </div>
    <script src="./js/vue.js"></script>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                // 1.定义变量
                uu: "", //新日常
                stu: [
                    { "id": 1, "name": "吃饭" },
                    { "id": 2, "name": "睡觉" },
                    { "id": 3, "name": "打豆豆" },
                ],
            },
            methods: {
                //5.函数
                adduser: function () {
                    //添加新日常的函数
                    var myid = this.stu.length;
                    this.stu.push({ "id": myid + 1, "name": this.uu });
                    //添加后清空input
                    this.uu = "";
                },
                // 7.添加删除的函数
                delFunc: function (index) {
                    confirm("确认要删除么？") && this.stu.splice(index, 1);
                },
            },
        });
    </script>
</body>
</html>